package net.xdclass.rbac_shiro;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Test;

public class QuickStartTest5_2 {


    @Test
    public void testAuthentication(){

        //创建SecurityManager工厂，通过配置文件ini创建
        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
        SecurityManager securityManager =factory.getInstance();
        //将securityManager 设置到当前环境中
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject =SecurityUtils.getSubject();
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("xdclass","123");
        subject.login(usernamePasswordToken);

        System.out.println("认证结果:"+subject.isAuthenticated());
        //是否有对应的角色
        System.out.println("是否有对应root角色:"+subject.hasRole("root"));
        //获取subject名
        System.out.println("获取subject名:"+subject.getPrincipal());
        //检查是否有对应的角色
        System.out.println("检查是否有对应的admin角色:"+subject.hasRole("admin"));
        //退出登录
        subject.logout();
        System.out.println("认证结果:"+subject.isAuthenticated());

    }
}
